package cn.controller;

import cn.pojo.App_version;
import cn.pojo.Dev_user;
import cn.service.app_version.App_versionService;
import cn.tools.Constants;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/app_version")
public class App_versionController {

    @Resource
    private App_versionService app_versionService;

    @RequestMapping("/select")
    public String select(Integer appid, HttpServletRequest request){
        List<App_version>list=app_versionService.getApp_versionById(appid);
        request.setAttribute("list",list);
        request.setAttribute("appId",appid);
        return "version_add";
    }

    @RequestMapping("/add")
    public  String add(MultipartFile file, HttpServletRequest request){
        String fileName="";
        String size=request.getParameter("versionSize");
        String appid=request.getParameter("appId");
        String status=request.getParameter("publishStatus");
        App_version app_version=new App_version();
        app_version.setVersionInfo(request.getParameter("versionInfo"));
        app_version.setVersionNo(request.getParameter("versionNo"));
        app_version.setVersionSize(Double.parseDouble(size));
        app_version.setAppId(Integer.parseInt(appid));
        app_version.setPublishStatus(Integer.parseInt(status));
        app_version.setCreationDate(new Date());
        app_version.setCreatedBy(((Dev_user)request.getSession().getAttribute(Constants.DEV_SESSION)).getId());
        app_version.setModifyDate(new Date());
        app_version.setModifyBy(((Dev_user)request.getSession().getAttribute(Constants.DEV_SESSION)).getId());
        if(!file.isEmpty()){
            String path=request.getSession().getServletContext().getRealPath("/upload");
            File files=new File(path);
            if (!files.exists()){
                files.mkdirs();
            }
            fileName=file.getOriginalFilename();
            String suffix= FilenameUtils.getExtension(fileName);
            int filesize=500000;
            if(file.getSize()>filesize){//上传大小不能超过500k
                request.setAttribute("fileUploadErro","上传大小不能超过500k");
                return "forward:/app_version/select";
            }else if(suffix.equalsIgnoreCase("apk")){
                fileName=System.currentTimeMillis()+"."+suffix;
                File file1=new File(path+"/"+fileName);
                app_version.setApkLocPath(request.getContextPath()+"/upload/"+fileName);
                app_version.setApkFileName("com.baidu-"+fileName);
                try {
                    file.transferTo(file1);
                }catch (Exception e){
                    e.printStackTrace();
                }
                app_versionService.add(app_version);
                return "redirect:/app_info/list";
            }else{
                request.setAttribute("appid",Integer.parseInt(appid));
                request.setAttribute("fileUploadErro","*上传文件格式不正确,要为(apk)");
                return "forward:/app_version/select?appid="+Integer.parseInt(appid);
            }
        }else {
            return "forward:/app_version/select";
        }

    }


    //ajax app版本修改检查
    @RequestMapping(value = "/change",produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public String change(String appid){
        List<App_version>list=app_versionService.getApp_versionById(Integer.parseInt(appid));
        Map<String,Boolean>map=new HashMap<>();
        if (list.size()==0){
            map.put("falg",true);
        }
        return JSONArray.toJSONString(map);
    }


    //跳转APP版本修改页面
    @RequestMapping("/version_change")
    public  String version_change(Integer appid,HttpServletRequest request){
        List<App_version>list=app_versionService.getApp_versionById(appid);
        System.out.println(list.get(0));
        App_version app_version=list.get(0);
        request.setAttribute("list",list);
        request.setAttribute("app_version",app_version);
        request.setAttribute("appid",appid);
        return "version_change";
    }

    @RequestMapping("/update")
    public  String update(MultipartFile file,App_version app_version, HttpServletRequest request){
        String fileName="";
        app_version.setModifyDate(new Date());
        app_version.setModifyBy(((Dev_user)request.getSession().getAttribute(Constants.DEV_SESSION)).getId());
        if(!file.isEmpty()){
            String path=request.getSession().getServletContext().getRealPath("/upload");
            File files=new File(path);
            if (!files.exists()){
                files.mkdirs();
            }
            fileName=file.getOriginalFilename();
            String suffix= FilenameUtils.getExtension(fileName);
            int filesize=500000;
            if(file.getSize()>filesize){//上传大小不能超过500k
                request.setAttribute("fileUploadErro","上传大小不能超过500k");
                return "forward:/app_version/select";
            }else if(suffix.equalsIgnoreCase("apk")){
                fileName=System.currentTimeMillis()+"."+suffix;
                File file1=new File(path+"/"+fileName);
                app_version.setApkLocPath(request.getContextPath()+"/upload/"+fileName);
                app_version.setApkFileName("com.baidu-"+fileName);
                try {
                    file.transferTo(file1);
                }catch (Exception e){
                    e.printStackTrace();
                }
                app_versionService.update(app_version);
                return "redirect:/app_info/list";
            }else{
                //request.setAttribute("appid",Integer.parseInt(appid));
                request.setAttribute("fileUploadErro","*上传文件格式不正确,要为(apk)");
                return "forward:/app_version/version_change?appid="+app_version.getAppId();
            }
        }else {
            return "forward:/app_version/version_change?appid="+app_version.getAppId();
        }

    }

    @RequestMapping("/update2")
    public  String update2(App_version app_version, HttpServletRequest request){
        app_version.setApkFileName("com.baidu-"+app_version.getApkFileName());
        app_versionService.update(app_version);
        return "redirect:/app_info/list";
    }

}
